require 'watir'
require 'watir'
include Watir
require 'test/unit'
require '../watir.config.rb'
require '../resources/functions.rb'


class TC_BlogPermissions_test < Test::Unit::TestCase
   
    def startup
      $GroupName= 'group1'
      $GroupDescription = 'This is the first group for creating blogs'
      $SectionName = 'BloG1'
      $AppKey ='BlogAppKey'
      $Owner = 'user1'
      $UserName = 'user1'
      $UserPassword ='pass123'
      $UserEmail = 'user@user.com'
      $PostTitle= 'Post1'
      $PostDescription = 'This is the first blog post'
      $PostTags = 'first,automated '
      @@ie = Watir::IE.start($SITE_URL)
    end
    
    def ie
      if defined? @@ie
        @@ie
      else
        startup
      end
    end
    
    def test_0000
        require 'win32/process'
        $pid = Process.create(
        :app_name      => 'ruby ../resources/clickers.rb',
        :creation_flags  => Process::DETACHED_PROCESS
        ).process_id
       
    end  
    
    def test_01_verifyHomeText
        assert(ie.contains_text(/Community Server/))		
    end
      
    def test_02_verifyLoginLinksExist
      if !(ie.link(:text, 'Sign in').exists?)
        ie.goto($SITE_URL + 'logout.aspx')
        ie.refresh
        ie.goto($SITE_URL)
        ie.refresh
      end
	  end
    
    def test_03_navigateToLogin
        ie.link(:text, 'Sign in').click
        assert(ie.contains_text("Sign in name"))
    end
	  
	  def test_04_populateLoginForm
        ie.text_field(:id, /username$/).value = $ADMIN_USER
        assert(ie.text_field(:id, /username$/).verify_contains($ADMIN_USER))
      
        ie.text_field(:id, /password$/).value = $ADMIN_PASSWORD
        assert(ie.text_field(:id, /password$/).verify_contains($ADMIN_PASSWORD))
	  end
	  
    def test_05_submitLoginForm
        if(ie.button(:id, /loginButton$/).exists?)
          ie.button(:id, /loginButton$/).click
        else
        ie.link(:id, /loginButton$/).click
        end
        test_01_verifyHomeText
    end
    
    def test_06_verifyControlPanel
        assert(ie.link(:text, /Control Panel/).exists?)
        ie.link(:text, /Control Panel/).click
        assert(ie.contains_text(/Announcements/))
        assert(ie.link(:text, /System Administration/).exists?)
        ie.link(:text, /System Administration/).click
        assert(ie.contains_text(/Announcements/))
    end
    
    def test_07_verifyControlPanelLinks
        assert_equal $SITE_URL + 'controlpanel/', ie.link(:text, 'Dashboard').href
        assert_equal $SITE_URL + 'controlpanel/setup.aspx', ie.link(:text, 'Administration').href
        assert_equal $SITE_URL + 'controlpanel/BlogAdmin/', ie.link(:text, 'Blog Administration').href
        assert_equal $SITE_URL + 'controlpanel/MediaGalleryAdmin/', ie.link(:text, 'Media Galleries Administration').href
        assert_equal $SITE_URL + 'controlpanel/Forums/', ie.link(:text, 'Forum Administration').href
        assert_equal $SITE_URL + 'controlpanel/HubAdmin/', ie.link(:text, 'Group Administration').href
        assert_equal $SITE_URL + 'controlpanel/Membership/membersearch.aspx?SelectedNavItem=BrowseMembers', ie.link(:text, 'Membership Administration').href
        assert_equal $SITE_URL + 'controlpanel/Settings/default.aspx', ie.link(:text, 'Site Administration').href
    end
    
   def test_08_navigateToBlogAdministration
        ie.goto($SITE_URL + 'controlpanel/membership/')
        assert(ie.contains_text('Membership Administration'))      
      end  
      
    def test_09_verifyLinksExist
      # Make sure that all of the links exist
        assert(ie.link(:text, 'Log Out').exists?)
        assert(ie.link(:text, 'Help').exists?)
        assert(ie.link(:text, 'Go to Site').exists?)
        assert(ie.link(:text, 'Dashboard').exists?)
        assert(ie.link(:text, 'Administration').exists?)
        assert(ie.link(:text, 'Membership Administration').exists?)
        assert(ie.link(:text, 'Telligent').exists?)
    end
      
    def test_10_createNewUser
        ie.goto($SITE_URL +'controlpanel/membership/UserAdd.aspx')
        assert(ie.text_field(:id,/username$/).exists?)
        ie.text_field(:id,/username$/).value = $UserName
        assert(ie.text_field(:id,/password$/).exists?)
        ie.text_field(:id,/password$/).value = $UserPassword
        password=ie.text_field(:id,/password$/).value
        assert(ie.text_field(:id,/password2$/).exists?)
        ie.text_field(:id,/password2$/).value =password
        assert(ie.text_field(:id,/email$/).exists?)
        ie.text_field(:id,/email$/).value = $UserEmail
        assert(ie.select_list(:id,/timezone$/).exists?)
        ie.select_list(:id,/timezone$/).select('(GMT +05:00) Islamabad, Karachi, Tashkent')
        assert(ie.button(:name,/createButton$/).exists?)
        sleep 3
        ie.button(:name,/createButton$/).click
        sleep 3
      end
      
    def test_11_navigateToCreateGroup 
        ie.goto($SITE_URL +'controlpanel/BlogAdmin/Groups.aspx')
        sleep 2
        assert(ie.button(:value , 'Add New Group').exists?)
        sleep 2
        ie.button(:value , 'Add New Group').click
        sleep 2
        assert(ie.text_field(:id,/GroupName$/).exists?)
        sleep 2
        ie.text_field(:id,/GroupName$/).value =$GroupName
        sleep 2
        assert(ie.text_field(:id,/GroupDesc$/).exists?)
        sleep 2
        ie.text_field(:id,/GroupDesc$/).value =$GroupDescription
        sleep 2
        assert(ie.button(:id,/SaveButton$/).exists?)
        ie.button(:id,/SaveButton$/).click
      end
      
    def test_12_navigateToCreateBlog
        ie.goto($SITE_URL +'controlpanel/BlogAdmin/sectionedit.aspx')
        assert(ie.text_field(:id,/SectionName$/).exists?)
        sleep 2
        ie.text_field(:id,/SectionName$/).value =$SectionName
        sleep 2
        assert(ie.text_field(:id,/AppKey$/).exists?)
        sleep 2
        ie.text_field(:id,/AppKey$/).value =$AppKey
        $AppKey1 = $AppKey.downcase
        sleep 2
        assert(ie.select_list(:id,/AdminGroupList$/).exists?)
        sleep 2
        ie.select_list(:id,/AdminGroupList$/).select($GroupName)
        sleep 2
        assert(ie.button(:id,/SaveButton$/).exists?)
        ie.button(:id,/SaveButton$/).click
      end
      
      def test_13_navigateToBlogs
        test_02_verifyLoginLinksExist
        test_03_navigateToLogin
        ie.text_field(:id, /username$/).value = $UserName
        ie.text_field(:id, /password$/).value = $UserPassword
        ie.button(:id, /loginButton$/).click
        sleep 2
        ie.goto($SITE_URL+'blogs/')
        sleep 2
        assert(ie.link(:text,'Blogs on this Site').exists?)
        sleep 2
        ie.link(:text,'Blogs on this Site').click
        sleep 2
        ie.goto($SITE_URL+'blogs/'+$AppKey1+'/default.aspx')
        sleep 2
        if(ie.link(:text,'Control Panel').exists?)
          puts 'Settings exists'
        end
      end
      
      def test_14_navigateToBlogPermissions
        test_02_verifyLoginLinksExist
        test_03_navigateToLogin
        test_04_populateLoginForm
        test_05_submitLoginForm
        sleep 3
        ie.goto($SITE_URL +'controlpanel/BlogAdmin/DefaultPermissionList.aspx')
        assert(ie.table(:index,4).exists?)
        table1 = ie.table(:index,4)
        rowcount = table1.row_count
        rowcount1 =1
        while rowcount1<=rowcount
            if (table1[rowcount1][1].text=='Registered Users')
            table1[rowcount1][2].checkbox(:id,/ViewCheckbox$/).set
            table1[rowcount1][3].checkbox(:id,/PostCheckbox$/).set
            table1[rowcount1][4].checkbox(:id,/ReplyCheckbox$/).set
            break
            end   
            rowcount1=rowcount1+1
          end
        assert(ie.button(:id,/SaveButton$/).exists?)
        ie.button(:id,/SaveButton$/).click
      end
      
      def test_15_navigateUserToCreatePost
        test_02_verifyLoginLinksExist
        test_03_navigateToLogin
        ie.text_field(:id, /username$/).value = $UserName
        ie.text_field(:id, /password$/).value = $UserPassword
        if(ie.button(:id, /loginButton$/).exists?)
          ie.button(:id, /loginButton$/).click
        else
        ie.link(:id, /loginButton$/).click
        end
        sleep 2
        ie.goto($SITE_URL+'blogs/')
        sleep 5
        assert(ie.link(:text,'Blogs on this Site').exists?)
        sleep 5 
        ie.link(:text,'Blogs on this Site').click
        sleep 5
        ie.goto($SITE_URL+'blogs/'+$AppKey1+'/default.aspx')
        sleep 5
        assert(ie.link(:text,'Control Panel').exists?)
        ie.link(:text,'Control Panel').click
        sleep 2
        ie.goto($SITE_URL+'controlpanel/blogs/posteditor.aspx?SelectedNavItem=NewPost')
        sleep 2
        assert(ie.text_field(:id,/CreatePost_ctl03_PostSubject$/).exists?)
        ie.text_field(:id,/CreatePost_ctl03_PostSubject$/).set($PostTitle)
        sleep 2
        ie.ie.Document.ParentWindow.execscript("tinyMCE.get('ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_CreatePost_ctl03_PostBody_ctl00_Editor').execCommand('mceSetContent', false,'"+$PostDescription+"')")
        sleep 2
        assert(ie.text_field(:id,/CreatePost_ctl03_PostTags_ctl00_Tags$/).exists?)
        ie.text_field(:id,/CreatePost_ctl03_PostTags_ctl00_Tags$/).value=$PostTags
        # Preview
        ie.ie.Document.parentWindow.execScript("ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_CreatePost_ctl03_EditorTabs.SelectTab(ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_CreatePost_ctl03_EditorTabs.GetTabAtIndex(4))")
        assert(ie.link(:id,/PostButton$/).exists?)
        sleep 5
        ie.link(:id,/PostButton$/).click  
        sleep 5
      end

       def test_16_deleteBlogPost
        test_02_verifyLoginLinksExist
        test_03_navigateToLogin
        test_04_populateLoginForm
        test_05_submitLoginForm
        sleep 3
        ie.goto($SITE_URL+'controlpanel/BlogAdmin/Blogs.aspx')
        assert(ie.table(:id,'BlogLists').exists?)
        table1 = ie.table(:id,'BlogLists')
        rowcount = table1.row_count
        rowcount1 =1
        ok = 0
        while rowcount1<=rowcount
            if (table1[rowcount1][1].link(:text,$SectionName).exists?)
              ok =1
               sleep 5
               table1[rowcount1][5].button(:value,'Manage' ).click
               break
            end   
            rowcount1=rowcount1+1
          end
        ie.goto($SITE_URL+'controlpanel/blogs/postlist.aspx')
        assert(ie.table(:id,'CommentListing').exists?)
        table1 = ie.table(:id, 'CommentListing')
        rowcount = table1.row_count
        rowcount1 =1
        ok = 0
        while rowcount1<=rowcount
            if (table1[rowcount1][1].link(:text,$PostTitle).exists?)
              ok =1
               sleep 5
               table1[rowcount1][5].button(:value, 'Delete').click
               break
             end   
         rowcount1=rowcount1+1
        end
      end
      
       def test_17_navigateToDeleteBlog
        ie.goto($SITE_URL+'controlpanel/BlogAdmin/Blogs.aspx')
        assert(ie.table(:id,'BlogLists').exists?)
        table1 = ie.table(:id,'BlogLists')
        rowcount = table1.row_count
        rowcount1 =1
        ok = 0
        while rowcount1<=rowcount
            if (table1[rowcount1][1].link(:text,$SectionName).exists?)
              ok =1
               sleep 5
               table1[rowcount1][5].button(:value,'Delete' ).click
               break
            end   
            rowcount1=rowcount1+1
        end
      end
      
      def test_18_navigateToDeleteBlogGroup
        ie.goto($SITE_URL+'controlpanel/BlogAdmin/Groups.aspx')
        assert(ie.table(:id,'GroupLists').exists?)
        table2 = ie.table(:id,'GroupLists')
        rows = table2.row_count
        rowcount2 =1
        ok = 0
        while rowcount2<=rows
            if (table2[rowcount2][1].link(:text,$GroupName).exists?)
              ok =1
               sleep 5
               table2[rowcount2][3].button(:value,'Delete' ).click
               break
            end   
         rowcount2=rowcount2+1
        end
      end
      
    def test_19_searchUser
        ie.goto($SITE_URL +'controlpanel/membership/membersearch.aspx?SelectedNavItem=BrowseMembers')
        assert(ie.text_field(:id,/searchText$/).exists?)
        ie.text_field(:id,/searchText$/).value ="*"
        assert(ie.select_list(:name,/PageList$/).exists?)
        ie.select_list(:name,/PageList$/).select('10')
        sleep 3
        assert(ie.button(:id,/searchButton$/).exists?)
        sleep 2
        ie.button(:id,/searchButton$/).click
      end
      
    def test_20_navigateToDeleteUser
        sleep 5
        table1 = ie.table(:index, 6)
        rowcount = table1.row_count
        rowcount1 =1
        ok =0
        while rowcount1<=rowcount
            if (table1[rowcount1][1].link(:text,$UserName).exists?)
              ok =1
               sleep 5
               table1[rowcount1][5].button(:value, 'Delete').click
               sleep 3
               break
             end   
         rowcount1=rowcount1+1
        end
       if(ok ==0)
         sleep 5
         ie.link(:text,'Next >').click
         sleep 5
         test_20_navigateToDeleteUser
       end
       sleep 5
       assert(ie.frame(:index,1).radio(:id,/AssignToAnonymous$/).exists?)
       ie.frame(:index,1).radio(:id,/AssignToAnonymous$/).set
       sleep 3
       assert(ie.frame(:index,1).button(:id,/DeleteUserButton$/).exists?)
       sleep 3
       ie.frame(:index,1).button(:id,/DeleteUserButton$/).click
       sleep 5
       ie.goto($SITE_URL +'controlpanel/membership/membersearch.aspx?SelectedNavItem=BrowseMembers')
       assert(ie.text_field(:id,/searchText$/).exists?)
       ie.text_field(:id,/searchText$/).value ="*"
       sleep 3
       assert(ie.button(:id,/searchButton$/).exists?)
       sleep 2
       ie.button(:id,/searchButton$/).click  
       if(ie.link(:text,$UserName).exists?)
       puts 'User Not Deleted'
       end  
     end  
     
     def test_21_navigateToBlogPermissions
        sleep 3
        ie.goto($SITE_URL +'controlpanel/BlogAdmin/DefaultPermissionList.aspx')
        assert(ie.table(:index,4).exists?)
        table1 = ie.table(:index,4)
        rowcount = table1.row_count
        sleep 5
         rowcount1 =1
        while rowcount1<=rowcount
            if (table1[rowcount1][1].text=='Registered Users')
            table1[rowcount1][3].checkbox(:id,/PostCheckbox$/).clear
            break
            end   
            rowcount1=rowcount1+1
          end
        assert(ie.button(:id,/SaveButton$/).exists?)
        ie.button(:id,/SaveButton$/).click
      end
      
    def test_99_Exit
        ie.close
      end
      
end  
    